import fs from "fs";
import path from "path";

/**
 * 动态获取系统的src/pages目录所有页面的入口，这里的入口
 *
 * @returns
 */
export function dynamicGetAllPageEntries() {
  const pagesPath = path.resolve(__dirname, "src/pages");
  const entries: Record<string, string> = {};

  // 使用 fs.readdirSync 读取 pages 目录
  const pages = fs.readdirSync(pagesPath);

  pages.forEach((page) => {
    const mainFile = path.join(pagesPath, page, "index.html");
    if (fs.existsSync(mainFile)) {
      entries[page] = path.resolve(
        process.cwd(),
        `src/pages/${page}/index.html`
      );
    }
  });

  return entries;
}

// 动态获取页面入口
export function getPageEntries(pages: string[]) {
  return pages.reduce((entries: Record<string, string>, page) => {
    entries[page] = path.resolve(process.cwd(), `src/pages/${page}/index.html`);
    return entries;
  }, {});
}

export const htmlSize = `<style>html{font-size:100px}</style>`;

export const adapterScript = `
<script>
  function refreshRem() {
    let docHtml = document.documentElement;
    let desW = 750; //针对宽度为750的设计稿
    let winWidth = docHtml.clientWidth; // html的宽度
    console.log(winWidth);
    docHtml.style.fontSize = (winWidth / desW) * 100 + "px"; 
  }

  refreshRem();

  window.addEventListener("resize", refreshRem);
</script>
`;
